//
//  UIButton+vertical.m
//  Customer
//
//  Created by jiangxiaobai on 2017/9/18.
//  Copyright © 2017年 tangang. All rights reserved.
//

#import "UIButton+vertical.h"

@implementation UIButton (vertical)

- (void)verticalImageAndTitle:(CGFloat)spacing andImage:(UIImage *)image
{
    CGSize imageSize = image.size;
    self.imageEdgeInsets = UIEdgeInsetsMake(0, spacing+5, 0.0, -spacing-5);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0,  imageSize.width);
    
}

- (void)verticalTopImageAndTitle:(CGFloat)spacing andImage:(UIImage *)image{
    [self setImage:image forState:UIControlStateNormal];
    CGSize imageSize = image.size;
    CGSize titleSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0, 0.0, - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, - (totalHeight - titleSize.height), 0);
}

+ (UIButton *)createBtnWithTitle:(NSString *)title{
    UIButton *btn=[[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
    return btn;
}

@end
